package com.ruoyi.quartz.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.service.ISysStudentService;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 学生信息模拟创建定时任务
 *
 * @author ruoyi
 */
@Component("studentTask")
public class StudentTask
{
    @Autowired
    private ISysStudentService sysStudentService;

    // 预定义姓氏列表
    private static final String[] LAST_NAMES = {"张", "王", "李", "赵", "刘", "陈", "杨", "黄", "周", "吴",
            "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗"};

    // 预定义名字列表
    private static final String[] FIRST_NAMES = {"伟", "芳", "娜", "秀英", "敏", "静", "丽", "强", "磊", "军",
            "洋", "勇", "艳", "杰", "娟", "涛", "明", "超", "秀兰", "霞",
            "平", "刚", "桂英", "鹏", "华", "红", "建", "文", "云", "兰"};

    private Random random = new Random();
    private int executionCount = 0; // 执行次数计数器

    public void createStudent()
    {
        executionCount++;
        long startTime = System.currentTimeMillis();
        System.out.println("【第" + executionCount + "次执行】开始时间：" + new Date());

        try {
            // 生成模拟学生数据
            SysStudent student = buildRandom();

            // 插入学生信息到数据库
            int result = sysStudentService.insertSysStudent(student);
            long endTime = System.currentTimeMillis();

            System.out.println("【第" + executionCount + "次执行】学生姓名：" + student.getStudentName() +
                    "，头像：" + student.getAvatar() +
                    "，插入结果：" + (result > 0 ? "成功" : "失败") +
                    "，耗时：" + (endTime - startTime) + "ms");

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            System.err.println("【第" + executionCount + "次执行】创建学生信息失败: " + e.getMessage() +
                    "，耗时：" + (endTime - startTime) + "ms");
            e.printStackTrace();
        }
    }

    /**
     * 构建随机学生信息
     */
    private SysStudent buildRandom() {
        SysStudent student = new SysStudent();

        // 随机姓名（确保唯一性）
        student.setStudentName(generateRandomName() + "_" + System.currentTimeMillis());

        // 随机头像（确保唯一性）- 关键修改！
        student.setAvatar(generateUniqueAvatar());

        // 随机简介
        String[] introductions = {
                "这是一名优秀的学生，学习成绩优异",
                "该生积极参与课外活动，表现突出",
                "学生在校期间表现良好，尊敬师长",
                "热爱学习，乐于助人的好学生",
                "在班级中担任班干部，责任心强"
        };
        student.setIntroduction(introductions[random.nextInt(introductions.length)]);

        // 随机性别：0代表男，1代表女
        student.setSex(String.valueOf(random.nextInt(2)));

        // 状态：0代表正常
        student.setStatus("0");

        // 删除标志：0代表存在
        student.setDelFlag("0");

        // 创建和更新信息
        student.setCreateBy("system");
        student.setUpdateBy("system");
        Date now = new Date();
        student.setCreateTime(now);
        student.setUpdateTime(now);

        // 随机备注
        String[] remarks = {
                "系统自动生成的模拟学生数据",
                "定时任务创建的测试学生记录",
                "用于系统功能测试的学生信息",
                "演示用的模拟学生数据",
                "测试环境自动生成的学生档案"
        };
        student.setRemark(remarks[random.nextInt(remarks.length)] + "，序号：" + executionCount);

        return student;
    }

    /**
     * 生成随机中文姓名
     */
    private String generateRandomName() {
        String lastName = LAST_NAMES[random.nextInt(LAST_NAMES.length)];
        String firstName = FIRST_NAMES[random.nextInt(FIRST_NAMES.length)];
        return lastName + firstName;
    }

    /**
     * 生成唯一头像路径 - 新增方法
     */
    private String generateUniqueAvatar() {
        // 方法1：使用时间戳 + 随机数（推荐）
        return "/images/avatar_" + System.currentTimeMillis() + "_" + random.nextInt(10000) + ".jpg";

        // 方法2：使用UUID（更确保唯一性）
        // return "/images/avatar_" + UUID.randomUUID().toString().substring(0, 8) + ".jpg";

        // 方法3：使用纳秒时间戳
        // return "/images/avatar_" + System.nanoTime() + ".jpg";
    }
}